﻿using SharpSoft.Maths;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF.DEMO.Pages
{
    /// <summary>
    /// CurvePage.xaml 的交互逻辑
    /// </summary>
    public partial class CurvePage : Page
    {
        public CurvePage()
        {
            InitializeComponent();
            num_tension.ValueChanged += Num_ValueChanged;
            num_continuity.ValueChanged += Num_ValueChanged;
            num_bais.ValueChanged += Num_ValueChanged;
            num_steplen.ValueChanged += Num_ValueChanged;
            canvas.SizeChanged += Canvas_SizeChanged;

        }

        private void Canvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.reDraw();
        }

        private void Num_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.reDraw();
        }

        public WriteableBitmap Bitmap
        {
            get { return (WriteableBitmap)GetValue(BitmapProperty); }
            set { SetValue(BitmapProperty, value); }
        }

        public static readonly DependencyProperty BitmapProperty =
            DependencyProperty.Register("Bitmap", typeof(WriteableBitmap), typeof(CurvePage), new PropertyMetadata(default(WriteableBitmap)));



        List<Vector2> points = new List<Vector2>();
        private void canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var p = e.GetPosition(canvas);
            AddPoint(p);
        }

        private void btn_clear_Click(object sender, RoutedEventArgs e)
        {
            points.Clear();
            reDraw();
        }


        private void reDraw()
        {
            Bitmap = new WriteableBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96, 96, PixelFormats.Pbgra32, BitmapPalettes.WebPaletteTransparent);
            using (Bitmap.GetBitmapContext())
            {

                Bitmap.Clear(Colors.Transparent);
                foreach (var p in points)
                {
                    var size = new Size(6, 6);
                    Bitmap.DrawRectangle(Convert.ToInt32(p.X - size.Width / 2), Convert.ToInt32(p.Y - size.Height / 2), Convert.ToInt32(p.X + size.Width / 2), Convert.ToInt32(p.Y + size.Height / 2), Colors.Green);
                }
                GenCurve();
            }

        }

        private void AddPoint(Point p)
        {
            points.Add(p.ToVector2());

            reDraw();
        }

        private void DrawPoint(Vector2 v)
        {
            Bitmap.FillRectangle(Convert.ToInt32(v.X), Convert.ToInt32(v.Y), Convert.ToInt32(v.X + 1), Convert.ToInt32(v.Y + 1), Colors.Red);
        }

        private void GenCurve()
        {
            var ps = CurveHepler.GetInterpolatedPoints(this.points.ToArray(), (float)num_tension.Value, (float)num_continuity.Value, (float)num_bais.Value, (float)num_steplen.Value);
            foreach (var item in ps)
            {
                DrawPoint(item);
            }
        }
    }
}
